<?php

declare(strict_types=1);

namespace App\Http\Controllers\Manage;

use App\Repositories\Manage\ComboSpecRepository;
use App\Repositories\Manage\MenuProductMappingRepository;
use App\Repositories\Manage\ProductRepository;
use App\Repositories\Manage\SpecRepository;
use App\Repositories\Manage\SupplierRepository;

class ProductController extends ManageBaseController
{

    private $productRepo = NULL;

    public function __construct(ProductRepository $productRepo)
    {
        parent::__construct();
        $this->productRepo = $productRepo;
    }

    public function index()
    {
        $filter = request()->all();
        if (!checkIsSuper(UID)) { //非超级会员
            $filter['shop_id'] = intval(getUserRelateShopId(UID));
        }
        $res = $this->productRepo->getList($filter, $this->pageNum, $this->pageSize);

        return retSuc($res);
    }

    public function detail($id)
    {
        $id = intval($id);
        if (!$id)
            return retFail("参数错误");
        $res = $this->productRepo->getOne($id);
        return retSuc($res);
    }

    /**
     * 清空待打印
     * @param $id
     */
    public function clear($id)
    {
        $id = intval($id);
        if (!$id)
            return retFail("参数错误");

        return $this->productRepo->clearPrinter($id);
    }

    public function options()
    {
        $shop_id = intval(request()->get("shop_id", 0));
        if (!checkIsSuper(UID)) { //非超级会员
            $shop_id = intval(getUserRelateShopId(UID));
        }

        $shops = (new  SupplierRepository())->getSupplierOptions($shop_id);

        return retSuc([
            "shops" => $shops
        ]);
    }

    /**
     *搜索商品
     */
    public function search_options()
    {
        $shop_id = intval(request()->get('shop_id', 0));
        $word = trim(request()->get('keyword', "") ?? "");
        if (!$shop_id)
            return retFail("请先选择店铺");

        $res = $this->productRepo->getProducts($word, $shop_id ?? 0);
        return retSuc($res);
    }

    public function update($id = null)
    {
        $data = request()->only([
            'shop_id', 'title', 'sort', "status", "sn", "category_id",
            "stall_id", "thumb", "min_buy", "limit_num", "product_type",
            "content", "product_feed", "product_taste", 'is_single_seller'
        ]);
        if (!checkIsSuper(UID)) {
            $shop_id = getUserRelateShopId(UID);

            if ($id) {
                $product = $this->productRepo->getOne(intval($id));
                if (empty($product))
                    return retFail("商品不存在");
                if ($product['shop_id'] != $shop_id) {
                    return retFail("您没有权限修改此店��的商品");
                }
            } else {
                $data['shop_id'] = $shop_id;
            }
        }

        $step = request()->post("step", 0);
        if ($step == 1) {
            $data = request()->only('price', 'bag_price', 'cost_price', 'stock', 'weight', 'spec_type', 'stock_cal_type', "product_type", "seller_time");
            $spec_groups = request()->post("skus");

            if ($data['product_type'] == 1) { //套餐选择总数
                $data['select_amount'] = intval(request()->post("select_amount"));
            }

            if (!$id)
                return retFail("请先完善商品基本信息");

            if (!empty($data['seller_time'])) {
                $data['start_time'] = $data['seller_time'][0] ?? "";
                $data['end_time'] = $data['seller_time'][1] ?? "";
                $data['seller_time'] = json_encode($data['seller_time']);
            }
//            unset($data['seller_time']);
        } else {
            if (!$data['shop_id']) {
                return retFail("所属店铺必填");
            }
        }

        $last_id = $this->productRepo->saveOrUpdate($data, $id ?? 0);
        if (!$last_id) {
            return retFail(!$id ? "新增失败" : "更新失败");
        }

        if ($spec_groups) {
            if ($data['product_type'] == 1) { //套餐
                (new ComboSpecRepository())->updateMappings(intval($last_id), $spec_groups);
            } else {
                (new SpecRepository())->updateMappings(intval($last_id), $spec_groups);
            }
        }

        (new MenuProductMappingRepository())->updateMenuRedisById($last_id);

        $res = $this->productRepo->getOne(intval($last_id));
        return retSuc($res);
    }

    /**
     * 复制套餐
     */
    public function copy($id = null)
    {
        $id = intval($id);
        if (!$id)
            return retFail("商品不存在");

        return $this->productRepo->copyProduct($id, UID);
    }

    public function delete($id)
    {
        $id = intval($id);

        if (!$id) {
            return retFail("参数错误");
        }

        return $this->productRepo->batchDelete($id);
    }

}
